package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.SensitiveMapper;
import com.heima.admin.service.SensitiveService;
import com.heima.model.admin.dto.SensitiveDto;
import com.heima.model.admin.pojo.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional//添加事务
public class SensitiveServiceImpl extends ServiceImpl<SensitiveMapper,AdSensitive> implements SensitiveService {


    //频道查询
    @Override
    public ResponseResult findByNameAndPage(SensitiveDto sensitiveDto) {

        if (sensitiveDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //1.检测参数
        sensitiveDto.checkParam();//自检,检查分页的数据


        //2.封装查询条件,分页条件,查询参数
        //设置分页参数

        IPage pageReq = new Page(sensitiveDto.getPage(), sensitiveDto.getSize());//用Ipage接口来接

        //构造lam条件
        LambdaQueryWrapper<AdSensitive> lambdaQueryWrapper = Wrappers.<AdSensitive>lambdaQuery();//lamdaquery要指定好泛型

        //有一个条件,判断是不是为空
        if (StringUtils.isNotBlank(sensitiveDto.getName())){
            lambdaQueryWrapper.like(AdSensitive::getSensitives,sensitiveDto.getName());
            //字段对应值
        }
        //封装分页响应结果
        //调分页的方法
        IPage page = this.page(pageReq, lambdaQueryWrapper);//得到分页的结果

        PageResponseResult pageResponseResult = new PageResponseResult(sensitiveDto.getPage(), sensitiveDto.getSize(), page.getTotal());

        pageResponseResult.setData(page.getRecords());


        return pageResponseResult;
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //判断敏感词有没有
        int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));

        if (count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"敏感词已存在");
        }

        if (StringUtils.isBlank(adSensitive.getSensitives())){

            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"敏感词不能为空");
        }


        adSensitive.setCreatedTime(new Date());

        this.save(adSensitive);
        //顺序不能反
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        //1.检查参数
        if (adSensitive.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.查询该数据
        AdSensitive sensitive = this.getById(adSensitive.getId());
        if (sensitive==null){

            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //3.如果不存在就提示用户


        //4.修改方法
        updateById(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delete(Integer id) {
        if (id==null){

            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdSensitive sensitive = getById(id);
        if (sensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);

        }
        removeById(id);

        return ResponseResult.okResult();
    }
}
